﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using ShopOnline.BLL;

namespace ShopOnline
{
    public partial class BasePage : System.Web.UI.Page
    {
        private string bannerKey = ShopOnline.BLL.BannerKey.Default;

        public string BannerKey
        {
            get { return bannerKey; }
            set { bannerKey = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public static OrderItems SelectedItems
        {
            get
            {
                if (HttpContext.Current.Session[CART] == null)
                {
                    OrderItems items =  new OrderItems();
                    items.LoadByPrimaryKey(-1);//load schema
                    HttpContext.Current.Session[CART] = items;
                }
                return HttpContext.Current.Session[CART] as OrderItems;
            }
            set
            {
                HttpContext.Current.Session[CART] = value;
            }
        }

        public static decimal TotalAmount
        {
            get
            {
                SelectedItems.Filter = "";
                SelectedItems.Rewind();
                decimal s = 0;
                for (int i = 0; i < SelectedItems.RowCount; i++)
                {
                    s += SelectedItems.TotalAmount;
                    SelectedItems.MoveNext();
                }
                SelectedItems.Rewind();
                return s;
            }
        }

        public static int TotalQuantity
        {
            get
            {
                SelectedItems.Filter = "";
                SelectedItems.Rewind();
                int s = 0;
                for (int i = 0; i < SelectedItems.RowCount; i++)
                {
                    s += SelectedItems.Quantity;
                    SelectedItems.MoveNext();
                }
                SelectedItems.Rewind();
                return s;
            }
        }

        public const string SORT_ASC = " asc";
        public const string SORT_DESC = " desc";
        public const string CAT = "category";
        public const string KEY = "key";
        public const string CART = "cart";
        public const string PRODUCT_IMAGE = "product_image";
        public const string PRD = "product";
        public const string NEW_ARRIVAL = "new_arrival";
        public const string DAILY_SPECIAL = "daily_special";
        public const string TAG = "tag";

        protected override void OnPreLoad(EventArgs e)
        {
            if (IsMobile(Request.UserAgent))
            {
                Response.Redirect(POSSystem.BLL.Utilities.GetConfigAppSetting("MobileSite"));
                return;
            }

            base.OnPreLoad(e);

            if (Branches.CurrentBranch == null)
            {
                int branchID = int.Parse(POSSystem.BLL.Utilities.GetConfigAppSetting("DefaultBranch"));
                Branches.CurrentBranch = new Branches();
                Branches.CurrentBranch.LoadByPrimaryKey(branchID);
            }

            this.MaintainScrollPositionOnPostBack = true;
        }

        public static bool IsMobile(string userAgent)
        {
            userAgent = userAgent.ToLower();

            return userAgent.Contains("iphone") |
                 userAgent.Contains("ppc") |
                 userAgent.Contains("windows ce") |
                 userAgent.Contains("blackberry") |
                 userAgent.Contains("opera mini") |
                 userAgent.Contains("mobile") |
                 userAgent.Contains("palm") |
                 userAgent.Contains("portable");
        }
    }
}